package store.lunangangster.nodeflow.core.component;

import com.alibaba.ttl.TransmittableThreadLocal;
import store.lunangangster.nodeflow.enums.NodeType;
import store.lunangangster.nodeflow.util.KeyGenerator;

/**
 * <h2>while循环Node</h2>
 * <p>如果业务中需要循环的次数是不确定的，可以使用该组件
 *
 * @author cwk
 * @version 1.0
 */
public abstract class WhileComponent extends NodeComponent {

  private static final ThreadLocal<String> ENTER_KEY_TL = new TransmittableThreadLocal<>();

  @Override
  public void process() {
    boolean isEnter = processWhile();
    String key = KeyGenerator.generate(NodeType.NODE_WHILE);
    ENTER_KEY_TL.set(key);
    this.getSlot().setData(key, isEnter);
  }

  /**
   * 用于每次循环时的进入判断
   * @return 是否进入循环
   */
  protected abstract boolean processWhile();

  @Override
  public String getWhileEnterKey() {
    return ENTER_KEY_TL.get();
  }

  @Override
  public void removeWhileEnterKey() {
    ENTER_KEY_TL.remove();
  }
}
